package jse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import jse.dao.Dao;
import jse.util.JseMap;


@Controller
public class DefaultAction {
	
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
		binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
//		binder.addCustomFormatter(new MapFormatter(""));
	}
	
	@Autowired
	Dao dao;
	
	@RequestMapping("/*")//一级js
	public Object home(HttpServletRequest req) {
		System.out.println(req.getServletPath());
		return null;
	}
	
	@RequestMapping("/**/")
	public Object index(HttpServletRequest req) {
		System.out.println(req.getServletPath()+"index");
		return null;
	}
	
	@RequestMapping("/**/{id:\\d+$}")
	public Object id(@PathVariable String id,HttpServletRequest req,HttpServletResponse resp) {
		String path=req.getServletPath();
		path=path.substring(0,path.lastIndexOf("/"));
		String suffix=path.substring(path.lastIndexOf("/"),path.length());
//		System.out.println("suffix"+suffix.substring(1));
		String action=(path+suffix);
		Route route=Routes.get("GET:"+action);
		System.out.println(Routes.routes);
		JseMap map=new JseMap();
		map.put("action", action);
		map.put("func", suffix.substring(1));
		map.put("tbl", req.getParameterMap());
		if(route==null) {
			JsRoute jsRoute=new JsRoute();
			Routes.add("GET:"+action, jsRoute);
			return jsRoute.run(map);
		}else {
			return route.run(map);
		}
	}
	
	@RequestMapping("/**")
	public Object page(HttpServletRequest req) {
		System.out.println(req.getServletPath()+"index");
		return null;
	}
	
	@RequestMapping("/**/*.{suffix}")
	public Object suffix(@PathVariable String suffix,HttpServletRequest req) {
		System.out.println(req.getServletPath());
		System.out.println(suffix);
		return null;
	}
}
